# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=czkawka
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cli"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gui"
         "${MINGW_PACKAGE_PREFIX}-krokiet")
pkgver=9.0.0
pkgrel=1
pkgdesc="Multi functional app to find duplicates, empty folders, similar images etc (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/qarmin/czkawka'
msys2_references=(
  'purl: pkg:cargo/czkawka_gui'
  'purl: pkg:cargo/czkawka_cli'
  'purl: pkg:cargo/krokiet'
)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk4"
         "${MINGW_PACKAGE_PREFIX}-libheif")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/qarmin/czkawka/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('2b2f419e1c733cad763eceb95eff28b1302e0926c247fdfd98e2f29f6f7866ee')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${_realname}-${pkgver}.tar.gz" || true
  cd "${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build \
    --bin krokiet \
    --bin czkawka_gui \
    --bin czkawka_cli \
    --release \
    --frozen \
    --features "heif libraw libavif"
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test \
    --bin czkawka_cli \
    --bin czkawka_gui \
    --bin krokiet \
    --release \
    --frozen \
    --features "heif libraw libavif"
}

package_czkawka-cli() {
  cd "${_realname}-${pkgver}"

  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-libheif")
  license=('spdx:MIT')
  pkgdesc+=" (CLI)"

  install -Dm644 czkawka_cli/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_cli/LICENSE"
  install -Dm755 target/release/czkawka_cli.exe "${pkgdir}${MINGW_PREFIX}/bin/czkawka_cli.exe"
}

package_czkawka-gui() {
  cd "${_realname}-${pkgver}"

  license=('spdx:MIT AND CC-BY-4.0')
  pkgdesc+=" (Desktop App)"

  install -Dm644 czkawka_gui/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_gui/LICENSE"
  install -Dm755 target/release/czkawka_gui.exe "${pkgdir}${MINGW_PREFIX}/bin/czkawka_gui.exe"
}

package_krokiet() {
  cd "${_realname}-${pkgver}"

  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-libheif")
  license=('spdx:MIT AND GPL-3.0-or-later')
  pkgdesc="New Czkawka frontend written in Slint (mingw-w64)"

  install -Dm644 krokiet/LICENSE{,_MIT_CODE} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/krokiet/"
  install -Dm755 target/release/krokiet.exe "${pkgdir}${MINGW_PREFIX}/bin/krokiet.exe"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
